package com.example.demo.service.impl;

import com.example.demo.core.Page;
import com.example.demo.entity.AdminUserLog;
import com.example.demo.mapper.AdminUserLogMapper;
import com.example.demo.service.AdminUserLogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 叮当猫的百宝箱
 * @since 1.0
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AdminUserLogServiceImpl implements AdminUserLogService {

    private final AdminUserLogMapper adminUserLogMapper;

    /**
     * 创建一条管理员操作日志
     *
     * @param adminUserLog 待添加的管理员操作日志
     */
    @Override
    public void create(AdminUserLog adminUserLog) {
        // 具体的操作行为不能为空
        if (adminUserLog != null && adminUserLog.getAction() != null) {
            LocalDateTime now = LocalDateTime.now();
            adminUserLog.setCreateTime(now);
            adminUserLog.setUpdateTime(now);
            adminUserLogMapper.insert(adminUserLog);
        }
    }

    /**
     * 获取日志列表
     *
     * @param pageNum  页码
     * @param pageSize 分页条数
     * @return 日志列表分页对象
     */
    @Override
    public Page<AdminUserLog> getAdminUserLogList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<AdminUserLog> adminUserLogList = adminUserLogMapper.findAll();
        PageInfo<AdminUserLog> pageInfo = PageInfo.of(adminUserLogList);
        return Page.restPage(pageInfo);
    }
}
